<template>
  <el-dialog
    v-model="dialogVisible"
    title="选择课次"
    width="80%"
    :before-close="handleDialogClose"
  >
    <div class="content">
      <el-form
        :model="innerQueryParams"
        ref="queryRef"
        :inline="true"
        v-show="showSearch"
        label-width="88px"
      >
        <el-form-item label="上课日期" prop="classDate">
          <el-date-picker
            v-model="classDate"
            type="daterange"
            range-separator="~"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            @change="classDateRangeChange"
            value-format="YYYY-MM-DD"
            style="width: 240px"
          />
        </el-form-item>
        <el-form-item label="授课课程" prop="courseId">
          <el-select
            v-model="innerQueryParams.courseId"
            placeholder="请选择授课课程"
            clearable
            style="width: 240px"
          >
            <el-option
              v-for="item in courseList"
              :key="item.id"
              :label="item.state === '1' ? item.name + '(已停用)' : item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="主班老师" prop="teacher">
          <el-select
            v-model="innerQueryParams.teacher"
            placeholder="请选择主班老师"
            clearable
            style="width: 240px"
          >
            <el-option
              v-for="item in teacherList"
              :key="item.value"
              :label="
                !item.isJob
                  ? item.label + '(已离职)'
                  : !item.isTeacher
                  ? item.label + '(非授课)'
                  : item.label
              "
              :value="item.value"
            />
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" icon="Search" @click="getInnerList"
            >搜索</el-button
          >
          <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
      <el-table
        v-loading="loading"
        :data="innerList"
        border
        highlight-current-row
        @current-change="handleCurrentChange"
      >
        <el-table-column>
          <template #default="scope">
            <el-radio v-model="scope.row.selected" :value="true"></el-radio>
          </template>
        </el-table-column>
        <el-table-column
          label="上课时间"
          align="center"
          prop="classDate"
          width="240"
        >
          <template #default="scope">
            {{
              scope.row.weekNum
                ? scope.row.classDate.slice(0, 10) +
                  "(周" +
                  {
                    1: "一",
                    2: "二",
                    3: "三",
                    4: "四",
                    5: "五",
                    6: "六",
                    7: "日",
                  }[scope.row.weekNum] +
                  ")" +
                  "  " +
                  scope.row.classStartTime +
                  "~" +
                  scope.row.classEndTime
                : scope.row.classDate.slice(0, 10) +
                  "  " +
                  scope.row.classStartTime +
                  "~" +
                  scope.row.classEndTime
            }}
          </template>
        </el-table-column>
        <el-table-column label="授课课程" align="center" prop="courseNameC" />
        <el-table-column label="上课教室" align="center" prop="classRoomName" />
        <el-table-column label="主班老师" align="center" prop="teacherName" />
        <el-table-column
          label="助教老师"
          align="center"
          prop="assistantName"
          width="160"
        />
        <el-table-column label="上课内容" align="center" prop="description" />
        <el-table-column label="授课课时" align="center" prop="classHour" />
      </el-table>
      <pagination
        v-show="total > 0"
        :total="total"
        v-model:page="innerQueryParams.pageNum"
        v-model:limit="innerQueryParams.pageSize"
        @pagination="getInnerList"
        size="small"
      />
    </div>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="handleDialogClose">取消</el-button>
        <el-button type="primary" @click="handleSubmit"> 确定 </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup>
import { getRollCallListAPI } from "@/api/registrar/class";
import { getCourseListAPI } from "@/api/registrar/course";
import { getPeopleList } from "@/api/studentManagement/index";

const props = defineProps({
  classId: {
    type: String,
  },
  studentId: {
    type: String,
  },
});

// 变量--------------------------------------------------------------------------------------------------------------------
const { proxy } = getCurrentInstance();

const emits = defineEmits(["refreshPage"]);
const loading = ref(false);
const dialogVisible = ref(false);
const innerTable = ref(null);
const innerList = ref([]);
const showSearch = ref(true);
const total = ref(0);
const currentRow = ref();

const courseList = ref([]);
const teacherList = ref([]);
const classDate = ref(null);

const data = reactive({
  innerQueryParams: {
    pageNum: 1,
    pageSize: 10,
    courseId: null,
    classStartTime: null,
    classEndTime: null,
    teacher: null,
  },
});

const { innerQueryParams } = toRefs(data);

function openSelector() {
  getTeacherList();
  getCourseList();
  resetInnerData();
  getInnerList();
  dialogVisible.value = true;
}

function handleDialogClose() {
  dialogVisible.value = false;
  currentRow.value = null;
}

function classDateRangeChange(event) {
  classDate.value = event;
  innerQueryParams.value.classStartTime = event ? event[0] : null;
  innerQueryParams.value.classEndTime = event ? event[1] : null;
}

function handleCurrentChange(currentSelect, oldSelect) {
  if (currentSelect) currentSelect.selected = true;
  if (oldSelect) oldSelect.selected = false;
  currentRow.value = currentSelect;
}

function resetInnerData() {
  innerQueryParams.value = {
    pageNum: 1,
    pageSize: 10,
    courseId: null,
    classStartTime: null,
    classEndTime: null,
    teacher: null,
  };
  classDate.value = null;
  innerTable.value && innerTable.value.setCurrentRow(null);
}

function resetQuery() {
  resetInnerData();
  getInnerList();
}

function getInnerList() {
  loading.value = true;
  getRollCallListAPI({
    ...innerQueryParams.value,
    classId: props.classId,
    studentIdMissed: props.studentId,
  }).then((response) => {
    response.rows.forEach((element) => {
      element.selected = false;
    });
    innerList.value = response.rows;

    total.value = response.total;
    loading.value = false;
  });
}

// 添加成员
function handleSubmit() {
  loading.value = false;
  dialogVisible.value = false;
  refreshParent(currentRow.value);
}

// 关联课程
function getCourseList() {
  getCourseListAPI({
    pageSize: 9999,
    pageNum: 1,
    state: "0",
    open: 1,
    //lessonType: "2",
  }).then((response) => {
    courseList.value = response.rows;
    loading.value = false;
  });
}

function getTeacherList() {
  loading.value = true;
  getPeopleList({ isTeach: 1 }).then((response) => {
    teacherList.value = response.data;
    loading.value = false;
  });
}

function refreshParent(val) {
  emits("refreshPage", val);
}

defineExpose({ openSelector }); // 用于父组件调用
</script>

<style lang="scss" scoped></style>
